#ifndef CHATCLIENT_H
#define CHATCLIENT_H

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <string>
#include <cstring>
#include <sys/select.h>
#include <iostream>
#include <json/json.h>

#define SERVER_PORT 9000
#define MAX_RECV_LINE 65536
#define MAX_SEND_LINE 1024
#define INVALID_SOCKET -1
#define SERVER_IP "10.211.55.6"

class ChatClient
{
public:
    static ChatClient& getChatClientInstance() {
        static ChatClient chat_client;
        return chat_client;
    }

    int createSocket();
    int createPipe();
    int run();
    ~ChatClient();
    void sendMsgSlots(std::string ip, std::string msg);
    void sendSynSlots();
    int checkConnState();
    void sendMultiMsgSlots(std::string multi_ip, std::string msg);
    void sendJoinMultiSlots(std::string multi_ip);
    void sendLeaveMultiSlots(std::string multi_ip);
private :
    ChatClient();
    ChatClient(const ChatClient&);
    ChatClient& operator=(const ChatClient &);
public :
    int conn_socket_;
    int server_port_;
    std::string server_ip_;
    int pipe_read_;
    int pipe_write_;
    char recv_msg_[MAX_RECV_LINE];
    //char send_msg_[MAX_SEND_LINE];
    std::string send_msg_;
    fd_set read_socket_set_;
    int max_fd_;
};

#endif // CHATCLIENT_H
